package com.yunquan.ohana.ui.mine

import android.app.Application
import android.os.Bundle
import androidx.camera.core.processing.SurfaceProcessorNode.In
import androidx.databinding.ObservableField
import androidx.lifecycle.LifecycleOwner
import com.yunquan.ohana.R
import com.yunquan.ohana.base.binding.command.BindingAction
import com.yunquan.ohana.base.binding.command.BindingCommand
import com.yunquan.ohana.base.bus.RxBus
import com.yunquan.ohana.base.bus.RxSubscriptions
import com.yunquan.ohana.base.bus.event.SingleLiveEvent
import com.yunquan.ohana.base.ui.BaseViewModel
import com.yunquan.ohana.entity.ContactEntity
import com.yunquan.ohana.entity.EventEntity
import com.yunquan.ohana.entity.EventType
import com.yunquan.ohana.entity.UserEntity
import com.yunquan.ohana.entity.UserManager
import com.yunquan.ohana.http.BaseHttp
import com.yunquan.ohana.http.observer.BaseObserver
import com.yunquan.ohana.http.transformer.CommonTransformer
import com.yunquan.ohana.ui.editProfile.EditProfileActivity
import com.yunquan.ohana.ui.web.WebActivity
import com.yunquan.ohana.utils.L
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.disposables.Disposable

class MineViewModel(application: Application) : BaseViewModel(application) {

    val ui = SingleLiveEvent<Int>()

    val nickname = ObservableField<String>()
    val avatar = ObservableField<String>()
    val defaultAvatar = ObservableField(R.mipmap.ic_defualt_avatar)

    val contact = ObservableField<String>()

    val onEditUserInfoClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            startActivity(EditProfileActivity::class.java)
        }
    })

    val onPrivacyPolicyClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val bundle = Bundle()
            bundle.putString("title", "Privacy Policy")
            bundle.putString("url", BaseHttp.mPrivacyPolicyUrl)
            startActivity(WebActivity::class.java, bundle)
        }
    })

    val onTermsUseClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            val bundle = Bundle()
            bundle.putString("title", "Terms Of Use")
            bundle.putString("url", BaseHttp.mTermsOfUse)
            startActivity(WebActivity::class.java, bundle)
        }
    })

    val onContactsClick = BindingCommand<Any>(object : BindingAction {
        override fun call() {
            ui.value = 0
        }
    })

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        getUserInfo()
        getContact()
    }

    override fun onEvent(event: EventEntity) {
        super.onEvent(event)
        if (event.type == EventType.GET_FRAMES) {
            getUserInfo()
        } else if (event.type == EventType.UPDATE_USER) {
            val user = UserManager.instance.getUser()
            user?.let { result ->
                result.nickname?.let {
                    nickname.set(it)
                }
                avatar.set(result.headImgUrl)
            }
        }
    }

    fun getUserInfo() {
        BaseHttp.api.getAppUser()
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<UserEntity>() {
                override fun onSuccess(result: UserEntity?) {
                    super.onSuccess(result)
                    if (result != null) {
                        UserManager.instance.setUser(result)
                        nickname.set(result.nickname ?: "Set Nickname")
                        avatar.set(result.headImgUrl)
                    }
                }
            })
    }

    private fun getContact() {
        BaseHttp.api.getContactInformation()
            .compose(CommonTransformer())
            .subscribe(object : BaseObserver<ContactEntity>() {
                override fun onSuccess(result: ContactEntity?) {
                    super.onSuccess(result)
                    result?.let {
                        contact.set(it.mail)
                    }
                }
            })
    }
}